package com.zzh.webhook.controller;

import com.zzh.webhook.model.GitlabPushEvent;
import com.zzh.webhook.service.WebhookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
public class WebController {

    private final WebhookService webhookService;

    @PostMapping("/webhook")
    public ResponseEntity<String> handleGitlabWebhook(
            @RequestBody GitlabPushEvent event,
            @RequestHeader(value = "X-Gitlab-Token", required = false) String token) {
        
        log.info("Received webhook from GitLab: {} for branch: {}", 
                event.getRepository().getName(), event.getBranchName());
        
        String result = webhookService.handlePushEvent(event, token);
        
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/status")
    public ResponseEntity<String> getStatus() {
        return ResponseEntity.ok("GitLab Webhook Service is running");
    }
    
    @PostMapping("/manual-build")
    public ResponseEntity<String> manualBuild(
            @RequestParam String branch,
            @RequestParam(required = false) String module) {
        
        log.info("Manual build requested for branch: {}, module: {}", branch, module);
        
        // 这里可以添加手动触发构建的逻辑
        
        return ResponseEntity.ok("Manual build triggered");
    }
}
